package cn.cmkj.auction.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.AuctionOrder;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class AuctionOrderListAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<AuctionOrder> mData;
    private ImageOptions imageOptions;

    public AuctionOrderListAdapter(Context context, List<AuctionOrder> mData) {
        this.context = context;
        this.mData = mData;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY).build();
    }

    @Override
    public int onSpan(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        int size = mData != null ? mData.size() : 0;
        return size;
    }

    public AuctionOrder getItem(int position) {
        if(mData == null || mData.size() <= 0)return null;
        AuctionOrder item = mData.get(position);
        return item;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_auction_order, parent, false);
        return new OrderViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final AuctionOrder item = mData.get(position);
        if(item.getGoodsImage() != null) {
            x.image().bind(((OrderViewHolder) holder).image, item.getGoodsImage(), imageOptions);
        }else {
            ((OrderViewHolder) holder).image.setImageResource(R.drawable.empty_photo);
        }
        ((OrderViewHolder) holder).textName.setText(item.getGoodsName());
        ((OrderViewHolder) holder).textPrice.setText("￥" + item.getAmount().doubleValue());
        //((OrderViewHolder) holder).textStatus.setBackgroundResource(R.drawable.order_status_red);
        ((OrderViewHolder) holder).textStatus.setVisibility(View.GONE);
        ((OrderViewHolder) holder).textTime.setText(item.getTime());
        ((OrderViewHolder) holder).textDisplayStatus.setText(item.getOrderStatus());
        ((OrderViewHolder) holder).auctionPayStatus.setText(item.getPayStatusStr());
    }

    class OrderViewHolder extends RecyclerView.ViewHolder {
        public TextView textTime;
        public TextView textDisplayStatus;
        public ImageView image;
        public TextView textName;
        public TextView textPrice;
        public TextView textStatus;
        public TextView auctionPayStatus;

        public OrderViewHolder(View itemView) {
            super(itemView);
            this.textTime = (TextView) itemView.findViewById (R.id.order_time);
            this.textDisplayStatus = (TextView) itemView.findViewById (R.id.order_display_status);
            this.image = (ImageView) itemView.findViewById (R.id.order_item_image);
            this.textName = (TextView) itemView.findViewById (R.id.order_item_name);
            this.textPrice = (TextView) itemView.findViewById (R.id.text_need_pay_price);
            this.textStatus = (TextView) itemView.findViewById (R.id.order_item_status);
            this.auctionPayStatus = (TextView) itemView.findViewById (R.id.auction_pay_status);
        }
    }
}
